package com.xvyy.tingshu.search.service.impl;


import com.xvyy.tingshu.album.client.AlbumInfoFeignClient;
import com.xvyy.tingshu.common.result.Result;
import com.xvyy.tingshu.model.album.TrackInfo;
import com.xvyy.tingshu.model.order.OrderInfo;
import com.xvyy.tingshu.model.search.AlbumInfoIndex;
import com.xvyy.tingshu.order.client.OrderInfoFeignClient;
import com.xvyy.tingshu.search.repository.AlbumInfoRepository;
import com.xvyy.tingshu.search.service.ItemService;
import com.xvyy.tingshu.search.service.MqOpsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Optional;

/**
 * ClassName: MqOpsServiceImpl
 * Package: com.xvyy.tingshu.search.service.impl
 *
 * @Description:
 * @Author: xvyy
 * @Create: 2025/1/19 - 17:52
 * @Version: v1.0
 */
@Service
public class MqOpsServiceImpl implements MqOpsService {

    @Autowired
    private ItemService itemService;
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;
    @Autowired
    private AlbumInfoRepository albumInfoRepository;


    @Override
    public void listenOnSaleAlbum(String albumId) {
        itemService.onAlbumEs(Long.valueOf(albumId));
    }

    @Override
    public void listenOffSaleAlbum(String albumId) {
        itemService.offAlbumEs(Long.valueOf(albumId));
    }

    @Override
    public void albumStatUpdate(String orderNo) {
        // 1. 远程调用order微服务，获取订单信息
        Result<OrderInfo> orderInfoResult = orderInfoFeignClient.getOrderInfo(orderNo);
        OrderInfo orderInfoData = orderInfoResult.getData();
        Assert.notNull(orderInfoData, "远程调用order微服务，获取订单信息失败");
        String itemType = orderInfoData.getItemType();
        Long albumId = null;
        // 2. 判断订单类型，获取专辑id
        switch (itemType) {
            case "1001" -> {
                albumId = orderInfoData.getOrderDetailList().get(0).getItemId();// 专辑
                saveForEs(albumId, orderInfoData);
            }
            case "1002" -> {
                // 声音
                Long trackId = orderInfoData.getOrderDetailList().get(0).getItemId();
                Result<TrackInfo> trackInfoResult = albumInfoFeignClient.getTrackInfoById(trackId);
                TrackInfo trackInfoData = trackInfoResult.getData();
                Assert.notNull(trackInfoData, "远程调用album微服务，获取声音信息失败");
                albumId = trackInfoData.getAlbumId();
                saveForEs(albumId, orderInfoData);
            }
        }//"1003" vip不用处理

    }

    private void saveForEs(Long albumId, OrderInfo orderInfoData) {
        Optional<AlbumInfoIndex> albumInfoIndex = albumInfoRepository.findById(albumId);
        albumInfoIndex.ifPresent(oldAlbumInfoIndex -> {
            Integer buyStatNum = oldAlbumInfoIndex.getBuyStatNum();
            oldAlbumInfoIndex.setBuyStatNum(buyStatNum + orderInfoData.getOrderDetailList().size());
            albumInfoRepository.save(oldAlbumInfoIndex);
        });
    }

}
